/**
 * Created by HAIWEI-LC on 2019/10/29
 */
package com.hw.paltfrom.service.comper;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwOaNoticeMapper;
import com.hw.paltfrom.mapper.HwbaseDataItemDetailMapper;
import com.hw.paltfrom.mapper.HwbaseDataItemMapper;
import com.hw.paltfrom.pojo.HwBaseDataItem;
import com.hw.paltfrom.pojo.HwBaseDataItemDetail;
import com.hw.paltfrom.pojo.HwOaNotice;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OaNoticeService {
    @Autowired
    HwOaNoticeMapper oaNoticeMapper;

    @Autowired
    HwbaseDataItemMapper dataItemMapper;

    @Autowired
    HwbaseDataItemDetailMapper dataItemDetailMapper;

    //添加
    public Map<String, Object> addOaNotice(HwOaNotice notice) {

        //初始化默认参数
        notice.setfId(RandomNumberUtil.getNumber());
        notice.settCount(0);
        notice.setfCreatedate(new Date());
        Integer insert = oaNoticeMapper.insert(notice);
        if (insert != 1){
            //添加失败
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }
    //删除
    public Map<String, Object> removeById(String fId) {

        Integer delete = oaNoticeMapper.deleteById(fId);
            if (delete != 1){
                //操作失败
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }

    //查询公告
    public Map<String, Object> findOaNotice(String fNoticetype, String fTitle, int page, int size) {

        //创建查询条件

        Wrapper<HwOaNotice> noticeWrapper = new EntityWrapper<>();

        //根据所传参数确定查询内容,模糊查询;

        noticeWrapper.like(StringUtils.isNotBlank(fTitle),"F_Title",fTitle)
                .eq(StringUtils.isNotBlank(fNoticetype),"F_NoticeType",fNoticetype)
                .orderBy("F_CreateDate",false);
        
        //根据前端传输数据分页
        PageHelper.startPage(page,size);
        List<HwOaNotice> hwOaNoticeList = oaNoticeMapper.selectList(noticeWrapper);
        PageInfo<HwOaNotice> hwOaNoticePageInfo = new PageInfo<>(hwOaNoticeList);

        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,hwOaNoticePageInfo);
    }

    //增加浏览次数

    public Map<String, Object> addcount(HwOaNotice hwOaNotice) {
        //浏览次数加1
        String fID = hwOaNotice.getfId();
        hwOaNotice.setfModifydate(new Date());
        HwOaNotice hwOaNotice_old = oaNoticeMapper.selectById(fID);

        hwOaNotice.settCount(hwOaNotice_old.gettCount()+1);
        Integer i = oaNoticeMapper.updateById(hwOaNotice);
        if (i != 1){
            return ResponeResultUtil.failNotice(HWSystemCode.FAIL, "更新失败");
        }

        hwOaNotice_old.settCount(hwOaNotice.gettCount());
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,hwOaNotice_old);
    }

    //查询公告分类
    public Map<String, Object> findTypes() {
        //查询公告分类字典类型id
        Wrapper<HwBaseDataItem> dataItemWrapper = new EntityWrapper<>();
        dataItemWrapper.eq("F_ItemName","公告分类");
        List<HwBaseDataItem> hwBaseDataItems = dataItemMapper.selectList(dataItemWrapper);
        if (hwBaseDataItems.size() == 0){
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        HwBaseDataItem hwBaseDataItem = hwBaseDataItems.get(0);
        String itemId = hwBaseDataItem.getfItemid();//公告分类字典类型id
        //根据字典分类id查询分类详情名称
        Wrapper<HwBaseDataItemDetail> dataItemDetailWrapper =  new EntityWrapper<>();
        dataItemDetailWrapper.eq("F_ItemId",itemId).orderBy("F_SortCode");
        List<HwBaseDataItemDetail> hwBaseDataItemDetails = dataItemDetailMapper.selectList(dataItemDetailWrapper);

        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,hwBaseDataItemDetails);
    }
}
